package com.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.student.bean.Student;
import com.student.dao.Dao;
import com.student.tools.MyUpload;

public class StudentAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Student.class);
	private Dao dao = null;

    public StudentAdd() {
        super();
        dao = new Dao();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action = request.getParameter("action");
		if ( "checkNo".equals(action) )
		{
			this.checkNo(request, response);
		}
		else if ( "submit".equals(action) )
		{
			try {
				this.submit(request, response);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		else if ( "".equals(action) || action == null ) {
			request.getRequestDispatcher("studentadd.jsp").forward(request, response);
		}
	}
	
	/*
	 * 新增用户提交操作
	 */
	private void submit(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException, ParseException {
		
		logger.info("User is visiting StudentAdd servlet to submit.");
		
		// 定义DateFormat
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		// 从页面获取学生信息
		String no = request.getParameter("no");
		String name = java.net.URLDecoder.decode(request.getParameter("name"), "UTF-8");
		String pwd = request.getParameter("pwd");
		String email = request.getParameter("email");
		String entranceTime = request.getParameter("entranceTime");
		String sex = java.net.URLDecoder.decode(request.getParameter("sex"), "UTF-8");
		String photo = request.getParameter("photo");
		String birth = request.getParameter("birth");
		String education = java.net.URLDecoder.decode(request.getParameter("education"), "UTF-8");
		
		// 提交学生信息到数据库
		Student stu = new Student();
		stu.setNo(Integer.parseInt(no));
		stu.setName(name);
		stu.setPwd(pwd);
		stu.setEmail(email);
		stu.setEntranceTime(format.parse(entranceTime));
		stu.setSex(sex);
		stu.setPhoto("images/student_head_image/"+no+"_"+photo);
		// 上传头像
		MyUpload upload = new MyUpload(request, no);
		upload.upload();
		stu.setBirth(format.parse(birth));
		stu.setEducation(education);
		stu.setStatus("在读");
		dao.addStudent(stu);
		
		// 响应提交
		String result = "提交成功！";
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		pw.print(result);
		pw.flush();
		pw.close();
		
	}

	/*
	 * 检查学号是否已经存在
	 */
	private void checkNo(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		
		logger.info("User is visiting StudentAdd servlet to check no.");
		String no = request.getParameter("no");
		request.getAttribute("");
		String result = "";
		if ( dao.checkStudent(Integer.parseInt(no)) ) {
			result = "该学号已经存在！";
		} else {
			result = "1";
		}
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		pw.print(result);
		pw.flush();
		pw.close();
	}

}
